package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import com.sun.xml.internal.bind.v2.TODO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: YddAdvposController
 * Description:
 * date: 2022/2/25 19:20
 *
 * @author zjf95
 * @since JDK 1.8
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class YddAdvposController {

    @Autowired
    private YddAdvposService yddAdvposService;

    @Autowired
    private YddAdvimgService yddAdvimgService;

    /**
    * Description: 分页查询
    * @author: zjf95
    * @date: 2022/2/28 20:13
    * @param:
    * @return:
    */
    @GetMapping("/list")
    public ResultData list(String advposdesc, String advposname, int page, int limit) {

        Page<YddAdvpos> yddAdvposPage = new Page<>(page, limit);
        Page<YddAdvDto> yddAdvDtoPage = new Page<>();

        LambdaQueryWrapper<YddAdvpos> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(advposdesc != null, YddAdvpos::getAdvposdesc, advposdesc);
        lambdaQueryWrapper.like(advposname != null, YddAdvpos::getAdvposname, advposname);
        yddAdvposService.page(yddAdvposPage, lambdaQueryWrapper);
        //BeanUtils.copyProperties(advposPage,yddAdvposPage);
        BeanUtils.copyProperties(yddAdvposPage, yddAdvDtoPage, "records");

        List<YddAdvpos> records = yddAdvposPage.getRecords();

        List<YddAdvDto> list = records.stream().map((item) -> {
            YddAdvDto yddAdvDto = new YddAdvDto();

            BeanUtils.copyProperties(item, yddAdvDto);

            Integer advimgpos = item.getId();

            LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YddAdvimg::getAdvimgpos, advimgpos);
            Integer count = yddAdvimgService.count(queryWrapper);
            log.info(count.toString());

            yddAdvDto.setAdvposnum(count);

            return yddAdvDto;
        }).collect(Collectors.toList());

        yddAdvDtoPage.setRecords(list);

        PageResult pageResult = PageResult.init(yddAdvDtoPage.getTotal(), yddAdvDtoPage.getSize(), yddAdvDtoPage.getCurrent(), yddAdvDtoPage.getRecords());

        return ResultData.ok("", pageResult);
    }

    @GetMapping("/all")
    public ResultData all() {

        List<YddAdvpos> list = yddAdvposService.list();

        return ResultData.ok("", list);

    }

    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestParam Long id){

        log.info("id为:",id);

        YddAdvpos yddAdvpos = yddAdvposService.getById(id);

        return ResultData.ok("",yddAdvpos);

    }
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos){

        log.info("修改后的对象为:{}",yddAdvpos);

        yddAdvposService.updateById(yddAdvpos);

        return ResultData.ok("","修改成功");
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody YddAdvpos yddAdvpos){

        log.info("要添加的对象为:{}", yddAdvpos);

        yddAdvposService.save(yddAdvpos);

        return ResultData.ok("","添加成功");
    }
}
